﻿using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace Admin.NET.Core
{
    public class qiniuHelper
    {
        static string AccessKey = "vysp4JzFlV4zkLtqbJ5DN3lOhH_NmRovw2a17r6j";
        static string SecretKey = "5dEsdHReTueN4Gakx69MJSGXLWoyDFdHAI1dbCiA";
        static string domain = "http://pictures.qinhezhiqian.com";
        // 空间名
        static string Bucket = "qhzq-img";
        static Mac mac = new Mac(AccessKey, SecretKey);

        public static void UploadFile(Stream stream,string key)
        {
            // 设置上传策略
            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Bucket + ":" + key;
            putPolicy.SetExpires(3600);
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            Config config = new Config();
            // 设置上传区域
            config.Zone = Zone.ZONE_CN_South;
            // 设置 http 或者 https 上传
            config.UseHttps = true;
            config.UseCdnDomains = true;
            config.ChunkSize = ChunkUnit.U512K;
            ResumableUploader target = new ResumableUploader(config);
            PutExtra extra = new PutExtra();
            //设置断点续传进度记录文件
            //extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(filePath, key);
            //Console.WriteLine("record file:" + extra.ResumeRecordFile);
            extra.ResumeRecordFile = "test.progress";
            //指定分片上传版本，默认分片上传 v1
            extra.Version = "v2";
            // 分片上传 v2 指定 part 大小，默认大小为4MB 分片大小范围为1 MB - 1 GB
            extra.PartSize = 4 * 1024 * 1024;
            // 指定分片并发大小，默认为1 
            extra.BlockUploadThreads = 4;
            HttpResult result = target.UploadStream(stream, key, token, extra);
        }

        public static string GetImg(string key)
        {
            string privateUrl = DownloadManager.CreatePrivateUrl(mac, domain, key, 3600);
            Console.WriteLine(privateUrl);
            return privateUrl;
        }
    }
}
